<?php

/**
 * Created by PhpStorm.
 * User: dumbledore
 * Date: 2018/2/26
 * Time: 下午4:42
 */
class Member_api extends CI_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->library('form_validation');//加载表单验证类库
        $this->load->helper('json');
        $this->load->helper('common');
        $this->load->model('user_model');
        $this->load->model('order_model');
        $this->load->model('bill_model');
        $this->load->library('email');
    }

    // 商城用户信息同步
    public function update_user_info_insert()
    {
        $post = $this->input->post();
        if (check_param($post, ['shop_customer_id', 'time_stamp', 'token'])) {
            // 验签
            $token = create_token($post, $this->config->item("token_const"));
            $check_res = check_token($token, $post['token'], $post['time_stamp'], $this->config->item("time_out"));
            if ($check_res['status'] == -1) {
                send_json(array('code' => -1, 'data' => '', 'msg' => $check_res['msg']));
            }

            $arr = [];
            if (isset($post['username'])) {
                // 昵称
                $arr['username'] = $post['username'];
            }
            if (isset($post['name'])) {
                // 联系人
                $arr['name'] = $post['name'];
            }
            if (isset($post['status'])) {
                // 状态
                $arr['status'] = $post['status'];
            }
            if (isset($post['tel'])) {
                // 手机号码
                $arr['tel'] = $post['tel'];
            }
            if (isset($post['tel'])) {
                // 电子邮箱
                $arr['email'] = $post['email'];
            }
            if (isset($post['password'])) {
                // 密码
                $arr['password'] = $post['password'];
            }
            if (empty($arr)) {
                send_json(array('code' => -1, 'data' => '', 'msg' => "失败，内容为空"));
            }

            $sql = 'select * from app_customer WHERE shop_user_id = ' . $post['shop_customer_id'];
            $res = $this->db->query($sql)->row_array();

            if (!$res) {
                $this->db->insert("app_customer", $arr);
                $bool = $this->db->insert("app_admin", $arr);
            } else {
//                $this->db->where(array("customer_id" => $res['id']))->update('app_manager_user', $arr);
//                $this->db->where(array("customer_id" => $res['id']))->update('app_admin', $arr);
                $bool = $this->db->where(array("shop_user_id" => $post['shop_customer_id']))->update('app_customer', $arr);
            }

            if ($bool) {
                send_json(array('code' => 0, 'data' => '', 'msg' => "用户信息同步成功"));
            } else {
                send_json(array('code' => -1, 'data' => '', 'msg' => "用户信息同步失败"));
            }
        } else {
            send_json(array('code' => -1, 'data' => '', 'msg' => "缺少参数"));
        }
    }


}